/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sparrsonat;

import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author leofidjeland -- lol vilket namn! :P //Ludvig
 */
public class TimeMachine extends Thread {

    private int sleepTime = 112; //ms
    private int mainTime = 0;
    private int slotLength = 100;
    private int thisSlot = 0;
    private SoundMachine soundMachine;

    public TimeMachine(SoundMachine soundMachineIn) {
        soundMachine = soundMachineIn;

    }

    @Override
    public void run() {
        while (true) {
            try {
                mainTime += 1;
                if (mainTime % slotLength == 0) {
                    trigger();
                }
                Thread.sleep(sleepTime);
            } catch (InterruptedException ex) {
                Logger.getLogger(TimeMachine.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    private void trigger() {
        //System.out.println("thisSlot: " + thisSlot);
        soundMachine.play(thisSlot);
        thisSlot = 0;
    }

    public void report() {
        thisSlot += 1;
    }
}
